package com.vehicle.common.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * JSR303 Validator(Hibernate Validator) 工具类
 * <p>
 * ConstraintViolation中包含propertyPath，message和invalidValue等信息.
 * 提供了各种convert方法，适合不同的i18n需求:
 * 1.List<String>，String 内容为message
 * 2.List<String>，String 内容为propertyPath+separator+message
 * 3.Map<propertyPath,message>
 * </p>
 * Created by yantingbin on 2017/7/18.
 */
public final class BeanValidators {
    private BeanValidators() {
        throw new IllegalAccessError("Utility class");
    }

    /**
     * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException.
     *
     * @param validator
     * @param object
     * @param groups
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void validateWithException(Validator validator, Object object, Class<?>... groups) {
        Set constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            throw new ConstraintViolationException(constraintViolations);
        }
    }

    /**
     * 辅助方法，转换ConstraintViolationException中的Set<ConstraintViolations>中为List<message>.
     *
     * @param e
     * @return
     */
    public static List<String> extractMessage(ConstraintViolationException e) {
        return extractMessage(e.getConstraintViolations());
    }

    /**
     * 辅助方法,转换Set<ConstraintViolation>为List<message>
     *
     * @param constraintViolations
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static List<String> extractMessage(Set<? extends ConstraintViolation> constraintViolations) {
        List<String> errorMessages = Lists.newArrayList();
        errorMessages.addAll(constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList()));
        return errorMessages;
    }

    /**
     * 辅助方法,转换ConstraintViolationException中的Set<ConstraintViolations>为Map<property,message>.
     *
     * @param e
     * @return
     */
    public static Map<String, String> extractPropertyAndMessage(ConstraintViolationException e) {
        return extractPropertyAndMessage(e.getConstraintViolations());
    }

    /**
     * 辅助方法,转换Set<ConstraintViolation> 为Map<property,message>
     *
     * @param constraintViolations
     * @return
     */
    public static Map<String, String> extractPropertyAndMessage(Set<? extends ConstraintViolation> constraintViolations) {
        Map<String, String> errorMessage = Maps.newHashMap();
        constraintViolations.forEach(v -> {
            errorMessage.put(v.getPropertyPath().toString(), v.getMessage());
        });
        return errorMessage;
    }

    /**
     * 辅助方法,转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath message>
     *
     * @param e
     * @return
     */
    public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e) {
        return extractPropertyAndMessageAsList(e.getConstraintViolations(), " ");
    }

    /**
     * 辅助方法,转换ConstraintViolationException中的Set<ConstraintViolations> 为List<propertyPath message>
     * @param constraintViolations
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations) {
        return extractPropertyAndMessageAsList(constraintViolations," ");
    }

    /**
     * 辅助方法，转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath+separator+message>.
     *
     * @param e
     * @param separator
     * @return
     */
    public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) {
        return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator);
    }

    /**
     * 辅助方法，转换Set<ConstraintViolation>为List<propertyPath+separator+message>
     *
     * @param constraintViolations
     * @param separator
     * @return
     */
    public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations, String separator) {
        List<String> errorMessages = Lists.newArrayList();
        errorMessages.addAll(constraintViolations.stream().map(v -> v.getPropertyPath() + separator + v.getMessage()).collect(Collectors.toList()));
        return errorMessages;
    }
}
